package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * @Classname MVSpeedAttrHandler
 * @Description MPLS-VPN产品 限速速率（M） <= 接入带宽（CE端信息）、端口速率（PE端信息） <= 接入带宽（CE端信息）
 * @Author zhang.song
 * @Date 2021-03-10 20:33
 */
public class MVSpeedAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    IQuerySceneInstService querySceneInstService;
    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.before(attr, inst, value, data);
        String serviceOfferId = data.getServiceOfferId();

        if(data.getTriggerStrategy() != null) {
            // 初始化加载页面时不校验，跳过初次加载时的属性 set 触发
            return;
        }

        if (KiteStringUtils.isEmpty(value)) {
            return;
        }
        String fieldName = attr.getFieldName();

        // 接入带宽（CE端信息）
        KiteAttr speedAttr = inst.getAttr("2_0000164");
        if(Objects.isNull(speedAttr) || KiteStringUtils.isEmpty(getValue(speedAttr, fieldName, value))) {
            return;
        }
        String speedValue = getValueDesc(speedAttr, fieldName, value);
        float speedFloatValue = Float.valueOf(speedValue.replaceAll("[^0-9\\.]", ""));
        if (speedValue.endsWith("G")) {
            speedFloatValue = speedFloatValue * 1024 * 1024;
        } else if (speedValue.endsWith("M")) {
            speedFloatValue = speedFloatValue * 1024;
        }



        // 限速速率（M）
        float speedLimitFloatValueCE = -1F;
        KiteAttr speedLimitAttrCE = inst.getAttr("200001733");
        if(Objects.nonNull(speedLimitAttrCE) && !KiteStringUtils.equals("2_0000036", fieldName) && KiteStringUtils.isNotEmpty(getValue(speedLimitAttrCE, fieldName, value))) {
            String speedLimitValueCE = getValueDesc(speedLimitAttrCE, fieldName, value);
            speedLimitFloatValueCE = Float.valueOf(speedLimitValueCE.replaceAll("[^0-9\\.]", ""));
            if (speedLimitValueCE.endsWith("G")) {
                speedLimitFloatValueCE = speedLimitFloatValueCE * 1024 * 1024;
            } else if (speedLimitValueCE.endsWith("M")) {
                speedLimitFloatValueCE = speedLimitFloatValueCE * 1024;
            }
        }

        // 端口速率（PE端信息）
        float speedLimitFloatValuePE = -1F;
        KiteAttr speedLimitAttrPE = inst.getAttr("2_0000036");
        if(Objects.nonNull(speedLimitAttrPE) && !KiteStringUtils.equals("200001733", fieldName) && KiteStringUtils.isNotEmpty(getValue(speedLimitAttrPE, fieldName, value))) {
            String speedLimitValuePE = getValueDesc(speedLimitAttrPE, fieldName, value);
            speedLimitFloatValuePE = Float.valueOf(speedLimitValuePE.replaceAll("[^0-9\\.]", ""));
            if (speedLimitValuePE.endsWith("G")) {
                speedLimitFloatValuePE = speedLimitFloatValuePE * 1024 * 1024;
            } else if (speedLimitValuePE.endsWith("M")) {
                speedLimitFloatValuePE = speedLimitFloatValuePE * 1024;
            }
        }
        if(speedLimitFloatValueCE < 0 && speedLimitFloatValuePE < 0) {
            return;
        }
            float speedLimitFloatValue = speedLimitFloatValuePE > speedLimitFloatValueCE ? speedLimitFloatValuePE : speedLimitFloatValueCE;
        if (speedLimitFloatValue > speedFloatValue) {
            String message;
            if(KiteStringUtils.equals("2_0000164", fieldName)) {
                message = "接入带宽必须大于等于" + (speedLimitFloatValuePE > speedLimitFloatValueCE ? speedLimitAttrPE.getCname() : speedLimitAttrCE.getCname());
            } else {
                message = (KiteStringUtils.equals("2_0000036", fieldName) ? speedLimitAttrPE.getCname() : speedLimitAttrCE.getCname()) + "限速速率必须小于等于接入带宽";
            }
            BssException.throwOut("", "", message);
        }
        if ("2826".equals(serviceOfferId)) {
            String speedoldValue = getValueDesc(speedAttr, speedAttr.getFieldName(), speedAttr.getOriginValue());
            float speedoldValues = Float.valueOf(speedoldValue.replaceAll("[^0-9\\.]", ""));
            if (speedoldValue.endsWith("G")) {
                speedoldValues = speedoldValues * 1024 * 1024;
            } else if (speedoldValue.endsWith("M")) {
                speedoldValues = speedoldValues * 1024;
            }
            float speedLimitFloatValuePEt = -1F;
            String speedLimitValuePEs = getValueDesc(speedLimitAttrPE, speedLimitAttrPE.getFieldName(), speedLimitAttrPE.getOriginValue());
            speedLimitFloatValuePEt = Float.valueOf(speedLimitValuePEs.replaceAll("[^0-9\\.]", ""));
            if (speedLimitValuePEs.endsWith("G")) {
                speedLimitFloatValuePEt = speedLimitFloatValuePEt * 1024 * 1024;
            } else if (speedLimitValuePEs.endsWith("M")) {
                speedLimitFloatValuePEt = speedLimitFloatValuePEt * 1024;
            }
            List<String> serviceOfferIdList = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "BUSI_CHANGE_SERVICEOFFERID");
            if(Objects.isNull(serviceOfferIdList)){
                return;
            }
            if (serviceOfferIdList.contains("1029")){
                // ce升速
                if(speedoldValues >= speedFloatValue) {
                    BssException.throwOut("", "","升速带宽新值不能小于原始带宽值");
                }
            }else if (serviceOfferIdList.contains("1030")){
                // ce降速
                if(speedFloatValue>= speedoldValues) {
                    BssException.throwOut("", "","降速带宽新值不能大于原始带宽值");
                }
            }else if (serviceOfferIdList.contains("1031")){
                // pe升速
                if( speedLimitFloatValuePEt>= speedLimitFloatValuePE) {
                    BssException.throwOut("", "","升速端口速率不能小于原始值");
                }
            } else if (serviceOfferIdList.contains("1032")){
                // pe降速
                if(speedLimitFloatValuePE >= speedLimitFloatValuePEt) {
                    BssException.throwOut("", "","降速端口速率不能大于原始值");
                }
            }
        }
    }

    /**
     * 取属性value值，如果属性=当前触发事件的属性，attr里的还是旧值，此时直接取入参里的value
     * @author zhang.song
     * @date 2021-02-23 15:24
     * @param attr
     * @param fieldName
     * @param value
     * @return java.lang.String
     */
    private static String getValue(KiteAttr attr, String fieldName, String value) {
        if (Objects.isNull(attr)) {
            return null;
        }

        if(fieldName.equals(attr.getFieldName())) {
            return value;
        } else {
            return attr.getValue();
        }
    }

    /**
     * 取属性显示值valueDesc，如果属性=当前触发事件的属性，attr里的还是旧值，此时用入参里的value查询显示值
     * @author zhang.song
     * @date 2021-02-23 15:24
     * @param attr
     * @param fieldName
     * @param value
     * @return java.lang.String
     */
    private static String getValueDesc(KiteAttr attr, String fieldName, String value) {
        if (Objects.isNull(attr)) {
            return null;
        }

        if(fieldName.equals(attr.getFieldName())) {
            List<KiteAttrValue> list = attr.getAttrValues();
            for (KiteAttrValue kiteAttrValue: list) {
                if(value.equals(kiteAttrValue.getAttrValue())) {
                    return kiteAttrValue.getAttrValueName();
                }
            }
            return null;
        } else {
            return attr.getValueDesc();
        }
    }
}
